package kr.co.happenstance.lib;

import java.net.URL;

import kr.co.happenstance.C;
import kr.co.happenstance.http.HttpRequestor;
import kr.co.happenstance.lib.vo.BillingCheckResultVo;
import kr.co.happenstance.lib.vo.BuyCheckResultVo;
import kr.co.happenstance.lib.vo.LibResultVo;
import kr.co.happenstance.lib.vo.MatchingAcceptYnResultVo;
import kr.co.happenstance.lib.vo.MatchingCountResultVo;
import kr.co.happenstance.lib.vo.MatchingInfoResultVo;
import kr.co.happenstance.lib.vo.MatchingRecipientAcceptCntResultVo;
import kr.co.happenstance.lib.vo.MatchingResultVo;
import kr.co.happenstance.lib.vo.RecipientAcceptListResultVo;
import kr.co.happenstance.lib.vo.RequestCountResultVo;
import kr.co.happenstance.lib.vo.RequestListResultVo;
import android.util.Log;

public class MatchingLib extends CommonLib {
	
	/**
	 * 매칭 카운트 조회 메소드
	 * 
	 * @param id
	 * @param latitude
	 * @param longitude
	 * @return
	 * @throws Exception
	 */
	public LibResultVo matchingCount(String id, String latitude, String longitude) throws Exception{
		MatchingCountResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_MATCHING_COUNT));
			httpRequestor.addParameter("id", id);
			httpRequestor.addParameter("latitude", latitude);
			httpRequestor.addParameter("longitude", longitude);
			//httpRequestor.addParameter("limitDistance", id);
			
			resultVo = super.send(MatchingCountResultVo.class);
			Log.d(TAG, "["+resultVo.getResult()+"]");
			Log.d(TAG, "["+resultVo.getMessage()+"]");
			Log.d(TAG, "["+resultVo.getMatchingCnt()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		return resultVo;
	}	
	
	
	
	/**
	 * 매칭 요청 메소드
	 * 
	 * @param id
	 * @param latitude
	 * @param longitude
	 * @param profileYN
	 * @return
	 * @throws Exception
	 */
	public LibResultVo matching(String id, String latitude, String longitude, String profileYN) throws Exception{
		MatchingResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_MATCHING));
			httpRequestor.addParameter("id", id);
			httpRequestor.addParameter("latitude", latitude);
			httpRequestor.addParameter("longitude", longitude);
			httpRequestor.addParameter("profileYN", profileYN);
			//httpRequestor.addParameter("limitDistance", id);
			
			resultVo = super.send(MatchingResultVo.class);
			Log.d(TAG, "["+resultVo.getResult()+"]");
			Log.d(TAG, "["+resultVo.getMessage()+"]");
			Log.d(TAG, "["+resultVo.getSendCnt()+"]");
			Log.d(TAG, "["+resultVo.getWaitMinute()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		return resultVo;
	}
	
	/**
	 * 수신자 수락 카운트 조회 메소드
	 * 
	 * @param matchingRequestSeq
	 * @return
	 * @throws Exception
	 */
	public LibResultVo matchingRecipientAcceptCount(int matchingRequestSeq) throws Exception{
		MatchingRecipientAcceptCntResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_MATCHING_RECIPIENT_ACCENT_COUNT));
			httpRequestor.addParameter("matchingRequestSeq", Integer.toString(matchingRequestSeq));
			
			resultVo = super.send(MatchingRecipientAcceptCntResultVo.class);
			Log.d(TAG, "["+resultVo.getResult()+"]");
			Log.d(TAG, "["+resultVo.getMessage()+"]");
			Log.d(TAG, "["+resultVo.getMatchingRecipientAcceptCnt()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		return resultVo;		
	}
	
	/**
	 * 매칭 관련 플래그 업데이트 메소드
	 * 
	 * @param id
	 * @param matchingRequestSeq
	 * @param updateGbn
	 * @return
	 * @throws Exception
	 */
	public LibResultVo matchingUpdate(String id, int matchingRequestSeq, String updateGbn) throws Exception{
		LibResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_MATCHING_UPDATE));
			httpRequestor.addParameter("id", id);
			httpRequestor.addParameter("matchingRequestSeq", Integer.toString(matchingRequestSeq));
			httpRequestor.addParameter("updateGbn", updateGbn);
			
			
			resultVo = super.send(LibResultVo.class);
			Log.d(TAG, "["+resultVo.getResult()+"]");
			Log.d(TAG, "["+resultVo.getMessage()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		return resultVo;			
	}
	
	
	/**
	 * 매칭 요청 정보 조회 메소드
	 * 
	 * @param matchingRequestSeq
	 * @param latitude
	 * @param longitude
	 * @return
	 * @throws Exception
	 */
	public LibResultVo matchingRequestInfo(int matchingRequestSeq, String latitude, String longitude) throws Exception{
		MatchingInfoResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_MATCHING_REQUEST_INFO));
			httpRequestor.addParameter("matchingRequestSeq", Integer.toString(matchingRequestSeq));
			httpRequestor.addParameter("latitude", latitude);
			httpRequestor.addParameter("longitude", longitude);
			
			resultVo = super.send(MatchingInfoResultVo.class);
			Log.d(TAG, "result["+resultVo.getResult()+"]");
			Log.d(TAG, "message["+resultVo.getMessage()+"]");
			Log.d(TAG, "id["+resultVo.getId()+"]");
			Log.d(TAG, "latitude["+resultVo.getLatitude()+"]");
			Log.d(TAG, "longitude["+resultVo.getLongitude()+"]");
			Log.d(TAG, "profileYn["+resultVo.getProfileYn()+"]");
			Log.d(TAG, "promiseDate["+resultVo.getPromiseDate()+"]");
			Log.d(TAG, "distance["+resultVo.getDistance()+"]");
			Log.d(TAG, "regDate["+resultVo.getRegDate()+"]");
			
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		return resultVo;
	}
	
	
	/**
	 * 요청자 수신동의 여부 조회 메소드
	 * 
	 * @param matchingRequestSeq
	 * @param recipientId
	 * @return
	 * @throws Exception
	 */
	public LibResultVo matchingAcceptYn(int matchingRequestSeq, String recipientId) throws Exception{
		MatchingAcceptYnResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_MATCHING_ACCEPT_YN));
			httpRequestor.addParameter("matchingRequestSeq", Integer.toString(matchingRequestSeq));
			httpRequestor.addParameter("recipientId", recipientId);
			
			resultVo = super.send(MatchingAcceptYnResultVo.class);
			Log.d(TAG, "result["+resultVo.getResult()+"]");
			Log.d(TAG, "message["+resultVo.getMessage()+"]");
			Log.d(TAG, "matchingAcceptYn["+resultVo.getMatchingAcceptYn()+"]");
	
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		return resultVo;		
	}
	
	/**
	 * 수신자 동의 리스트 조회 메소드
	 * 
	 * @param matchingRequestSeq
	 * @param latitude
	 * @param longitude
	 * @return
	 * @throws Exception
	 */
	public LibResultVo recipientAcceptList(int matchingRequestSeq, String latitude, String longitude) throws Exception{
		RecipientAcceptListResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_RECIPIENT_ACCEPT_LIST));
			httpRequestor.addParameter("matchingRequestSeq", Integer.toString(matchingRequestSeq));
			httpRequestor.addParameter("latitude", latitude);
			httpRequestor.addParameter("longitude", longitude);
			
			resultVo = super.send(RecipientAcceptListResultVo.class);
			Log.d(TAG, "result["+resultVo.getResult()+"]");
			Log.d(TAG, "message["+resultVo.getMessage()+"]");
			Log.d(TAG, "acceptList["+resultVo.getAcceptList()+"]");
			
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}		
		return resultVo;
	}
	
	/**
	 * 요청 개수 가져오는 메소드
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public LibResultVo requestCount(String id) throws Exception{
		RequestCountResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_REQUEST_COUNT));
			httpRequestor.addParameter("recipientId", id);
			
			resultVo = super.send(RequestCountResultVo.class);
			Log.d(TAG, "result["+resultVo.getResult()+"]");
			Log.d(TAG, "message["+resultVo.getMessage()+"]");
			Log.d(TAG, "requestCount["+resultVo.getRequestCount()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		return resultVo;
	}		
	
	/**
	 * 요청 목록 가져오는 메소드
	 * 
	 * @param id
	 * @param latitude
	 * @param longitude
	 * @return
	 * @throws Exception
	 */
	public LibResultVo requestList(String id, String latitude, String longitude) throws Exception{
		RequestListResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_REQUEST_LIST));
			httpRequestor.addParameter("recipientId", id);
			httpRequestor.addParameter("latitude", latitude);
			httpRequestor.addParameter("longitude", longitude);
			
			resultVo = super.send(RequestListResultVo.class);
			Log.d(TAG, "result["+resultVo.getResult()+"]");
			Log.d(TAG, "message["+resultVo.getMessage()+"]");
			Log.d(TAG, "requestList["+resultVo.getRequestList()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}		
		return resultVo;		
	}
	
	/**
	 * 결제여부 가져오는 메소드
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public LibResultVo billingCheck(String id) throws Exception{
		BillingCheckResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_BILLING_CHECK));
			httpRequestor.addParameter("id", id);
			
			resultVo = super.send(BillingCheckResultVo.class);
			Log.d(TAG, "result["+resultVo.getResult()+"]");
			Log.d(TAG, "message["+resultVo.getMessage()+"]");
			Log.d(TAG, "isBilling["+resultVo.isBilling()+"]");
			Log.d(TAG, "developerPayload["+resultVo.getDeveloperPayload()+"]");
			Log.d(TAG, "seq["+resultVo.getSeq()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}		
		return resultVo;		
	}	
	
	/**
	 * 정상구매 여부 가져오는 메소드
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public LibResultVo buyCheck(String developerPayload, int developerPayloadSeq, String orderId) throws Exception{
		BuyCheckResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_BUY_CHECK));
			httpRequestor.addParameter("developerPayload", developerPayload);
			httpRequestor.addParameter("developerPayloadSeq", Integer.toString(developerPayloadSeq));
			httpRequestor.addParameter("orderId", orderId);
			
			resultVo = super.send(BuyCheckResultVo.class);
			Log.d(TAG, "result["+resultVo.getResult()+"]");
			Log.d(TAG, "message["+resultVo.getMessage()+"]");
			Log.d(TAG, "isValidate["+resultVo.isValidate()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}		
		return resultVo;		
	}		
	
	
}
